package org.aristotle.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aristotle.common.utils.AristotleBeanUtils;
import org.aristotle.demo.model.entity.Favorite;
import org.aristotle.demo.model.entity.User;
import org.aristotle.demo.mapper.FavoriteMapper;
import org.aristotle.demo.service.IBookService;
import org.aristotle.demo.service.IFavoriteService;
import org.aristotle.demo.service.IUserService;
import org.aristotle.demo.model.vo.BookVO;
import org.aristotle.demo.model.vo.FavoriteVO;
import org.aristotle.demo.model.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, Favorite> implements IFavoriteService {

    @Autowired
    private IBookService bookService;

    @Autowired
    private IUserService userService;

    @Override
    public Page<FavoriteVO> queryPageList(Long userId, Long bookId, Integer current, Integer size) {

        QueryWrapper<Favorite> queryWrapper = new QueryWrapper();

        // 判断id是否为空
        if (userId != null) {
            queryWrapper.eq("user_id",userId);
        }

        if (bookId != null) {
            queryWrapper.eq("book_id",bookId);
        }

        Page<Favorite> page = this.page(new Page<Favorite>(current,size), queryWrapper);

        // 转为VO
        Page<FavoriteVO> result = AristotleBeanUtils.transitionBean(page, Page<FavoriteVO>::new);
        result.setRecords(AristotleBeanUtils.copyListProperties(page.getRecords(),FavoriteVO::new));

        result.getRecords().forEach(item -> {
            // 查询Book
            BookVO bookVO = this.bookService.getById(item.getBookId());
            item.setBook(bookVO);

            // 查询User
            UserVO userVO = this.userService.getById(item.getUserId());
            item.setUser(userVO);
        });

        return result;
    }

    @Override
    public FavoriteVO getById(Long id) {
        Favorite favorite = this.baseMapper.selectById(id);

        FavoriteVO favoriteVO = AristotleBeanUtils.transitionBean(favorite, FavoriteVO::new);

        // 查询Book
        BookVO bookVO = this.bookService.getById(favoriteVO.getBookId());
        favoriteVO.setBook(bookVO);

        // 查询User
        UserVO userVO = this.userService.getById(favoriteVO.getUserId());
        favoriteVO.setUser(userVO);

        return favoriteVO;
    }

    @Override
    @Transactional
    public Boolean removeById(Long id) {
        return super.removeById(id);
    }

    @Override
    @Transactional
    public Boolean removeById(Long userId, Long bookId) {
        QueryWrapper<Favorite> queryWrapper = new QueryWrapper();
        // 判断id是否为空
        if (userId == null || bookId == null) {
            return null;
        }
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("book_id",bookId);

        return this.remove(queryWrapper);
    }
}
